<?php 
include ("Pattern.php");
include ("../models/DBConnection.php");

class Kmeans {
	var $_centroids;
	var $_patterns;
	var $_k;
	var $_times; 
	var $_clusters;

	function Kmeans($__k = 3, $_times = 3){
		$this->_k = $__k; 
		$this->_patterns = array();
		$this->_centroids = array();
		$this->_clusters = array();
		
		$query = new query("select * from  features");
		foreach ($query->v as $reg){
			$pattern = new Pattern("$reg->titulo");
			$pattern->_pattern[] = ($reg->bitrate == '') ? 0 : $reg->bitrate;
			$pattern->_pattern[] = ($reg->vbitrate == '') ? 0 : $reg->vbitrate;
			$pattern->_pattern[] = ($reg->abitrate == '') ? 0 : $reg->abitrate;
			$pattern->_pattern[] = ($reg->framecount == '') ? 0 : $reg->framecount;
			$pattern->_pattern[] = ($reg->framerate == '') ? 0 : $reg->framerate;
			$pattern->_pattern[] = ($reg->framewidth == '') ? 0 : $reg->framewidth;
			$pattern->_pattern[] = ($reg->frameheight == '') ? 0 : $reg->frameheight;
			$pattern->_pattern[] = ($reg->audiochannels == '') ? 0 : $reg->audiochannels;
//			echo $pattern->_label . " ";
//			for ($i = 0; $i < count($pattern->_pattern); $i++)
//				echo $pattern->_pattern[$i] . " ";
//			echo "</br>";	
			$this->_patterns[] = $pattern; 
		}
		// Making Kmeans
		$counter = 0;
		$this->__centroid_selection();
		while($counter < $_times){
			echo "</br>Iteration " . $counter . "</br>";
			$this->__evolution();
			$this->__centroid_updating();
			$counter++;
		}
	}

	function  __centroid_selection (){
		srand(time());
		$rand = rand(0,count($this->_patterns));
		$counter = 0;
		while ($counter < $this->_k){
			while ($this->_patterns[$rand]->_selected == true) 
				$rand = rand(0,count($this->_patterns));
			$this->_patterns[$rand]->_selected =  true;
			$this->_centroids[] = $this->_patterns[$rand];
			$counter++;
		}
		for ($i = 0; $i < $this->_k; $i++){
			for ($k = 0; $k < count($this->_centroids[$i]->_pattern); $k++)
				echo $this->_centroids[$i]->_pattern[$k] . " ";
		echo "</br>";
		}
	}
	
	function __evolution (){
		unset($this->_clusters);
		for ($i = 0; $i < count($this->_patterns); $i++){
			$index = 0;
			$min_dist = $this->_patterns[$i]->euclidean_distance($this->_centroids[$index]->_pattern);
			for ($k = 1; $k < $this->_k; $k++){
				$new_dist = $this->_patterns[$i]->euclidean_distance($this->_centroids[$k]->_pattern);
				if ($new_dist < $min_dist){
					$min_dist = $new_dist;
					$index = $k;
				}
			}
			$this->_clusters[$index][] = $this->_patterns[$i];
		}
		
		for ($k = 0; $k < $this->_k; $k++){
			echo count($this->_clusters[$k]) . "</br>";
		}
	}
	
	function  __centroid_updating (){
		for ($k = 0; $k < $this->_k; $k++){
			$pattern = new Pattern();
			for ($i = 0; $i  < count($this->_centroids[0]->_pattern); $i++)
				$pattern->_pattern[] = 0.0;
			for ($i = 0; $i < count($this->_clusters[$k]); $i++){
				for ($t = 0; $t  < count($this->_clusters[$k][$i]->_pattern); $t++){
					$pattern->_pattern[$t] += $this->_clusters[$k][$i]->_pattern[$t];
				}
			}
			for ($i = 0; $i  < count($this->_centroids[0]->_pattern); $i++){
				$pattern->_pattern[$i] /= count($this->_clusters[$k]);
				echo $pattern->_pattern[$i] ." ";
				}
			echo "</br>";
			$this->_centroids[$k] = $pattern;
		}
	}
}
$k = new Kmeans(5,50);
?>